package pv.control;

import com.google.android.gms.internal.cn;

import android.app.LauncherActivity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class SubCategoryView {
	protected LinearLayout layout;
	protected Context context;
	protected SubCategoryAdapter adapter;
	onSubCategoryListener listener;

	public SubCategoryAdapter getAdapter() {
		return adapter;
	}

	public void setAdapter(SubCategoryAdapter adapter) {
		this.adapter = adapter;
		this.adapter.setLayout(this);
	}

	public SubCategoryView(Context context, ViewGroup parentView) {
		this.context = context;
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		layout = new LinearLayout(context);
		layout.setLayoutParams(params);
		layout.setOrientation(LinearLayout.HORIZONTAL);
		parentView.addView(layout);

	}

	public void notifyDataSetChanged() {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (layout != null) {
			layout.removeAllViews();
			for (int i = 0; i < adapter.getCount(); i++) {

				layout.addView(adapter.getView(inflater, i), i);
			}

		}

	}

	public int getChildCount() {
		// TODO Auto-generated method stub
		return layout != null ? layout.getChildCount() : 0;
	}

	public View getChildAt(int index) {
		return layout != null ? layout.getChildAt(index) : null;
	}

	public void addViews(View[] views) {

		if (views != null && views.length > 0) {
			clear();
			for (int i = 0; i < views.length; i++) {
				addView(views[i], i);
			}
		}
	}

	public void update() {
		layout.invalidate();

	}

	public void clear() {
		layout.removeAllViews();
	}

	public void addView(View view, int index) {
		if (view != null) {
			layout.addView(view, index);

		}
	}

	public void setOnSubCategorySelected(onSubCategoryListener listener) {
		this.listener = listener;
	}

	public void onSubCategorySelected(int position) {
		listener.onSubCategorySelected(position);
	}

	public interface onSubCategoryListener {
		void onSubCategorySelected(int position);
	}

}
